<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="级别" prop="level">
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="级别"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--        下拉框 选择团长级别 1、2、3 -->
        <el-select
          v-model="queryParams.level"
          placeholder="请选择级别"
          size="large"
          style="width: 240px"
        >
          <el-option
            v-for="item in levelList"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="团长姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入团长姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.$index + 1 }} &lt;!&ndash; 使用 $index 获取当前行的索引，并加1以从1开始计数 &ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="负责人ID" align="center" prop="id" />
      <el-table-column label="级别" align="center" prop="level" >
<!--        根据级别来分成一级、二级、三级-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level == 1">一级</el-tag>
          <el-tag v-if="scope.row.level == 2">二级</el-tag>
          <el-tag v-if="scope.row.level == 3">三级</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="所属拼团点" align="center" prop="communityName" />
      <el-table-column label="团长姓名" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="身份证" align="center" prop="idcard" width="200"/>
      <el-table-column label="邀请码" align="center" prop="invitationCode" />
      <el-table-column label="拥有店铺数量" align="center" prop="storeCount" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="storeStatus0(scope.row)">{{scope.row.storeCount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="拥有拼团数量" align="center" prop="groupCount" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="grouplist(scope.row)">{{scope.row.groupCount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费金额" align="center" prop="money" />
      <el-table-column label="录入时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:leader:edit']"
          >修改</el-button>
        </template>‘
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[1]"
        background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加或修改负责人管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="团长姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入团长姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
<!--          <el-input v-model="form.phone" placeholder="请输入电话" />-->
<!--          换成下拉框选择手机号-->
          <el-select
            v-model="form.phone"
            placeholder="请选择手机号"
            size="large"
            style="width: 240px"
          >
            <el-option
              v-for="item in phoneList"
              :label="item.phone"
              :value="item.phone"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证" prop="idcard">
<!--          <el-input v-model="form.idcard" placeholder="请输入身份证" />-->
<!--          换成下拉框选择身份者号-->
          <el-select
            v-model="form.idcard"
            placeholder="请选择身份证"
            size="large"
            style="width: 240px"
          >
            <el-option
              v-for="item in identityList"
              :label="item.idcard"
              :value="item.idcard"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="级别" prop="level">
<!--         下拉框选择级别 -->
          <el-select
            v-model="form.level"
            placeholder="请选择级别"
            size="large"
            style="width: 240px"
          >
            <el-option
              v-for="item in ji"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="拼团点" prop="idcard">
        <el-select
          v-model="form.communityName"
          placeholder="Select"
          size="large"
          style="width: 240px"
        >
          <el-option
            v-for="item in communityList"
            :key="item.id"
            :label="item.title"
            :value="item.title"
          />
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLeader, getLeader, delLeader, addLeader, updateLeader ,show,showPhone,showIdentity} from "@/api/system/leader";

export default {
  name: "Leader",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 级别
      ji:[
      //   1、一级、2二级、3三级
        {
          label: "一级",
          value: 1
        },
        {
          label: "二级",
          value: 2
        },
        {
          label: "三级",
          value: 3
        }
      ],
      // 全部的手机号
      phoneList: [],
      // 全部的身份者
      identityList: [],
      // 团长级别
      levelList:[
      // 1一级、2二级、3三级
        {
          label: "一级",
          value: 1
        },
        {
          label: "二级",
          value: 2
        },
        {
          label: "三级",
          value: 3
        }
      ],

      // 负责人管理表格数据
      leaderList: [],
      // 查询全部的拼点地点
      communityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        level: null,
        name: null,
        phone: null,
        password: null,
        idcard: null,
        money: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.showa();
  //   查询负责人全部的手机号
    this.showPhone();
  //   查询全部的身份证
    this.showIdentity();
  },
  methods: {
    /** 查询负责人管理列表 */
    getList() {
      this.loading = true;
      listLeader(this.queryParams).then(response => {
        var reg = /^(\d{4})\d+(\d{4})$/;
        response.data.list.forEach(item => {
          item.idcard = item.idcard.replace(reg, '$1********$2');
        });
        this.leaderList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        level: null,
        name: null,
        phone: null,
        password: null,
        idcard: null,
        invitationCode: null,
        money: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加负责人管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLeader(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑负责人："+this.form.name+"(ID "+this.form.id+")";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLeader(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeader(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 点击跳转到store//传送leaderid
    storeStatus0(row){
    this.$router.push({
      name: 'StoreMange',
      params: {
        leaderId: row.id,
      }
    })
    },
    // 点击跳转到group//传送leaderid
    grouplist(row){
      this.$router.push({
        name: 'GroupManage',
        params: {
          leaderId: row.id,
        }
      })
    },
    // 查询全部的手机号
    showPhone() {
      showPhone().then(response => {
      this.phoneList=response.data
      });
    },
    // 查询全部的身份者号
    showIdentity() {
      showIdentity().then(response => {
      this.identityList=response.data
      });
    },
    showa() {
      show().then(response => {
      this.communityList=response.data
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除负责人管理编号为"' + ids + '"的数据项？').then(function() {
        return delLeader(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/leader/export', {
        ...this.queryParams
      }, `leader_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
